package com.jeff.android.lab.android.storage;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.widget.TextView;

import androidx.core.os.EnvironmentCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.duff.download.okdownload.LiteDownload;
import com.duff.download.okdownload.interfaces.LiteDownloadListener;
import com.duff.download.okdownload.utils.MD5Utils;
import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.permission.CheckPermission;
import com.jeff.permission.PermissionItem;
import com.jeff.permission.PermissionListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * @author: duff
 * @date: 2019/1/17
 * @since: 1.0.0
 */
public class StorageFragment extends BaseFragment {
    @BindView(R.id.ptr) PtrClassicFrameLayout mPtr;
    @BindView(R.id.rlv) RecyclerView mRecyclerView;

    List<Model> mStorages = new ArrayList<>();

    class Model {
        String desc;
        String api;
        String path;

        public Model(String desc, String api, String path) {
            this.desc = desc;
            this.api = api;
            this.path = path;
        }
    }

    @Override
    protected int getRootLayout() {
        return R.layout.android_fragment_storage;
    }

    @Override
    protected void initView() {
        mPtr.setEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mStorageAdapter);
    }

    BaseQuickAdapter mStorageAdapter =
            new BaseQuickAdapter<Model, BaseViewHolder>(R.layout.android_item_storage_path) {
                @Override
                protected void convert(BaseViewHolder helper, final Model item) {
                    TextView tv_title = helper.getView(R.id.tv_title);
                    tv_title.setText(item.desc);

                    TextView tv_desc = helper.getView(R.id.tv_desc);
                    tv_desc.setText(item.path);
                }
            };

    @Override
    protected void initData() {
        /*
         * 应用内部存储
         * */
        mStorages.add(new Model("应用内部缓存目录",
                "Context.getCacheDir", mContext.getCacheDir().getAbsolutePath()));
        mStorages.add(new Model("应用内部文件目录",
                "Context.getFilesDir", mContext.getFilesDir().getAbsolutePath()));

        /*
         * 应用外部私有存储(读写不需要申请权限)，保存在该目录下的图片不会显示在图库中
         * */
        mStorages.add(new Model("应用外部缓存目录",
                "Context.getExternalCacheDir", mContext.getExternalCacheDir().getAbsolutePath()));
        mStorages.add(new Model("应用外部文件目录(需要指定类型，如DIRECTORY_MUSIC)",
                "Context.getExternalFilesDir", mContext.getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath()));

        /*
         * 系统内部存储
         * */
        mStorages.add(new Model("data分区",
                "Environment.getDataDirectory", Environment.getDataDirectory().getAbsolutePath()));
        mStorages.add(new Model("缓存目录",
                "Environment.getDownloadCacheDirectory", Environment.getDownloadCacheDirectory().getAbsolutePath()));
        mStorages.add(new Model("system分区",
                "Environment.getRootDirectory", Environment.getRootDirectory().getAbsolutePath()));

        /*
         * 系统外部存储
         * */
        mStorages.add(new Model("外部存储根目录",
                "Environment.getExternalStorageDirectory", Environment.getExternalStorageDirectory().getAbsolutePath()));
        mStorages.add(new Model("外部公共目录(需要指定类型，DIRECTORY_DOWNLOADS)",
                "Environment.getExternalStoragePublicDirectory", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()));

        mStorageAdapter.setNewData(mStorages);


        // state
        String state = EnvironmentCompat.getStorageState(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
        String externalStorageState = Environment.getExternalStorageState();


        mkdirs();
    }


    private void mkdirs() {
        if (Environment.MEDIA_MOUNTED
                .equals(Environment.getExternalStorageState())) {
            final String cacheDir = mContext.getExternalCacheDir().getAbsolutePath();
            final String cacheDirImage = cacheDir + File.separator + "image";
            final File file = new File(cacheDirImage);
            file.mkdirs();


            CheckPermission.instance(mContext)
                    .check(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                            new PermissionListener() {
                                @Override
                                public void permissionGranted() {
                                    String externalStorageDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
                                    String path = externalStorageDirectory + File.separator + "123";
                                    new File(path).mkdirs();

                                    LiteDownload.download("http://www.psyxx.net/upload/2018/04/1524122896135490.jpg",
                                            path + File.separator + MD5Utils.MD5.get32MD5String("1524122896135490") + ".jpg",
                                            new LiteDownloadListener() {
                                                @Override
                                                public void onError(Exception e) {

                                                }

                                                @Override
                                                public void onSuccess(File file) {
                                                    mContext.sendBroadcast(
                                                            new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                                                    Uri.parse("file://" + file.getAbsolutePath())));
                                                }
                                            });
                                }

                                @Override
                                public void permissionDenied() {

                                }
                            });

            LiteDownload.download("http://img5.xiazaizhijia.com/walls/20150417/mid_84422024ff063d3.jpg",
                    cacheDirImage + File.separator + MD5Utils.MD5.get32MD5String("mid_84422024ff063d3") + ".jpg",
                    new LiteDownloadListener() {
                        @Override
                        public void onError(Exception e) {

                        }

                        @Override
                        public void onSuccess(File file) {
                            mContext.sendBroadcast(
                                    new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                                            Uri.parse("file://" + file.getAbsolutePath())));
                        }
                    });
        }
    }

}
